Pex & Moles: The Basics

Parameterized Unit Testing With Pex

Creating a simple class to test


Let Pex generate a starter test suite


Inspect Pex's generated top-level test suite


Run Pex Explorations...

Pex found the Y2K Bug!

Distinguish between the one-time generated top-level test suite and these test cases that are regenerated with every exploration.


Good code coverage

Tell Pex the exception is not a bug

Pex will update the top-level generated test

TODO - update this  - show PexAllowedException

Rerun explorations

And everything is green


And code coverage is still 100%

Introducing Moles

Create a new class to test that isn't parameterized


Run Pex Explorations...


Pex didn't generate much


And according to the code coverage report, it missed the Y2K bug

Moles to the rescue!   Generate Moles Types (Stubs)

Compile the stubs


Look at the Moles console output


Inspect the generated stubs

Inspect the DateTime Stub


Import the Moles Stubs into the Pex-Generated Top-Level Test Suite

Turn the test into a parameterized test using Moles stubs


Delete the old generated tests

For some reason Pex complains and won't regenerate its tests because we manually modified one of its PexMethods.


Rerun Pex Explorations


Pex rediscovered the Y2K bug with Moles' help



Again, tell Pex to ignore the ApplicationException


Note the PexAllowedException Annotation



Tell Pex to rerun its explorations


Everything is green!


And the code coverage report confirms the Y2K bug was triggered

How does dynamic routing work with the debugger?

Set a break point in the code under test.


Run the unit tests under the debugger


Confirm the Moles Stub was Injected